/*
 * @Author: your name
 * @Date: 2022-03-17 13:38:32
 * @LastEditTime: 2022-03-28 18:31:03
 * @LastEditors: Please set LastEditors
 * @FilePath: \VUE\app\src\store\home\index.js
 */

import { reqGetRegisterCode, reqRegisterUser, reqUserLogin } from "@/api";

//仓库存储数据的
const state = {
    registerCode: ''
};
//修改state的唯一手段
const mutations = {
    GETREGISTERCODE(state, registerCode) {
        console.log(registerCode);
        state.registerCode = registerCode
    }
};
//处理action，书写业务逻辑，异步处理
const actions = {
    //获取注册验证码
    async getRegisterCode({ commit }, phone) {
        let result = await reqGetRegisterCode(phone)
        if (result.code == 200) {
            commit('GETREGISTERCODE', result.data)
        }
    },
    //完成注册
    async finishRegister({ commit }, { phone, password, code }) {
        console.log('三个参数', phone, password, code);
        let result = await reqRegisterUser({ phone, password, code })
        if (result.code == 200) {
            console.log(result);
        } else {
            alert('注册错误!')
            console.log(result);
        }
    },
    //用户登录
    async userLogin({ commit }, { phone, password }) {
        let result = await reqUserLogin({ phone, password })
        if (result.code == 200) {
            console.log(result);
        }else {
            alert('登录失败!')
        }
    }
};

//getters理解为计算属性让组件获取仓库数据更方便
const getters = {};



//暴露一个store实例
export default {
    state,
    mutations,
    actions,
    getters

}
